package com.hnas.mrch.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.exception.EntityNotFoundException;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.mrch.dao.GenerateMrchTwoCodeDAO;
import com.hnas.mrch.orm.GenerateMrchTwoCode;
import com.hnas.mrch.service.dto.DTOGenerateMrchTwoCode;
import com.hnas.mrch.service.dto.search.DTOGenerateMrchTwoCodeSearch;
import com.hnas.mrch.service.mapper.GenerateMrchTwoCodeMapper;

@Component("generateMrchTwoCodeService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class GenerateMrchTwoCodeServiceImpl implements GenerateMrchTwoCodeService {

	@Autowired
	private GenerateMrchTwoCodeDAO generateMrchTwoCodeDAO;

	@Override
	public DTOGenerateMrchTwoCode getGenerateMrchTwoCode(long id) {
		GenerateMrchTwoCode vo = this.generateMrchTwoCodeDAO.find(id);
		if (null != vo) {
			return GenerateMrchTwoCodeMapper.getInstance().mapToValue(vo);
		} else {
			return null;
		}
	}

	@Override
	public DTOGenerateMrchTwoCode getMrchTwoCodeByMrchId(String mrchId) {
		GenerateMrchTwoCode vo = this.generateMrchTwoCodeDAO.getMrchTwoCodeByMrchId(mrchId);
		if (null != vo) {
			return GenerateMrchTwoCodeMapper.getInstance().mapToValue(vo);
		} else {
			return null;
		}
	}

	@Override
	public DTOPagedList<DTOGenerateMrchTwoCode> search(DTOGenerateMrchTwoCodeSearch con) {
		PageBean<GenerateMrchTwoCode> page = new PageBean<GenerateMrchTwoCode>();
		page.setPageNo(con.getPageNo());
		page.setPageSize(con.getPageSize());
		page.setTotalCount(con.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		sb.append(" and t.valid = ?" + j++);
		paras.add(true);
		if (StringUtil.isNotEmpty(con.getMrchId())) {
			sb.append(" and t.mrchName like ?" + j++);
			paras.add("%" + con.getMrchName().trim() + "%");
		}
		if (StringUtil.isNotEmpty(con.getTermNo())) {
			sb.append(" and t.termNo like ?" + j++);
			paras.add("%" + con.getTermNo().trim() + "%");
		}
/*		if (StringUtil.isNotEmpty(con.getMrchId())) {
			sb.append(" and t.mrchId like ?" + j++);
			paras.add("%" + con.getMrchId().trim() + "%");
		}*/
		if (sb.length() > 0) {
			sb.delete(0, 5);
			sb.insert(0, " from GenerateMrchTwoCode t where ");
		} else {
			sb.append(" from GenerateMrchTwoCode t");
		}
		sb.append(" order by t.updateTime DESC");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) " + sb);
		this.generateMrchTwoCodeDAO.queryByPage(page, paras.toArray());
		DTOPagedList<DTOGenerateMrchTwoCode> pageList = new DTOPagedList<DTOGenerateMrchTwoCode>();
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		List<DTOGenerateMrchTwoCode> ls = new ArrayList<DTOGenerateMrchTwoCode>();
		for (GenerateMrchTwoCode gm : page.getList()) {
			DTOGenerateMrchTwoCode dto = new DTOGenerateMrchTwoCode();
			dto = GenerateMrchTwoCodeMapper.getInstance().mapToValue(gm);
			ls.add(dto);
		}
		pageList.setList(ls);
		return pageList;
	}

	@Override
	public void saveGenerateMrchTwoCode(DTOGenerateMrchTwoCode dto) throws EntityNotFoundException {
		GenerateMrchTwoCode vo = this.generateMrchTwoCodeDAO.find(dto.getId());
		if (null != vo) {
			BeanUtils.copyProperties(dto, vo, new String[] { "id", "createTime", "valid" });
			vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
			this.generateMrchTwoCodeDAO.merge(vo);
		} else {
			vo = new GenerateMrchTwoCode();
			BeanUtils.copyProperties(dto, vo, new String[] { "id" });
			vo.setValid(true);
			vo.setCreateTime(DateUtil.dateTimeToString(new Date()));
			vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
			this.generateMrchTwoCodeDAO.persist(vo);
		}
	}

	@Override
	public void disableGenerateMrchTwoCode(long id) {
		GenerateMrchTwoCode vo = this.generateMrchTwoCodeDAO.find(id);
		vo.setValid(false);
		vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
		this.generateMrchTwoCodeDAO.merge(vo);
	}

	@Override
	public DTOGenerateMrchTwoCode getMrchTwoCodeByMrchIdAndTermNo(String mrchId, String termNo) {
		GenerateMrchTwoCode vo = this.generateMrchTwoCodeDAO.getMrchTwoCodeByMrchIdAndTermNo(mrchId, termNo);
		if (null != vo) {
			return GenerateMrchTwoCodeMapper.getInstance().mapToValue(vo);
		} else {
			return null;
		}
	}
}
